package co.fitstart.admin.web.controller.trade.product;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import co.fitstart.admin.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.DataService;
import co.fitstart.entity.product.ProductCategory;
import co.fitstart.entity.product.ProductType;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("")
@MenuSetting("menu-ecommerce")
public class ProductCategoryController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProductCategoryController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<ProductCategory> productCategoryService;
    
    /**
     * List all product category map by type
     * 
     * @return
     */
    @RequestMapping("/product-categories")
    @MenuSetting("submenu-product-category-management")
    public ModelAndView list() {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/category/product-category-list");
        
        List<ProductCategory> result = productCategoryService.listAll(ProductCategory.class);
        Map<String, List<ProductCategory>> productCategories = CollectionUtils.constructMapValuesFromList(result, "type");
        
        modelAndView.addObject("productCategories", productCategories);
        
        return modelAndView;
    }
    
    /**
     * View create panel
     * 
     * @param type
     * @return
     */
    @RequestMapping(value = "/product-categories/create", method = RequestMethod.GET)
    public ModelAndView create(@RequestParam(value = "type") String type) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/category/product-category-create");
        
        modelAndView.addObject("type", ProductType.aliasOf(type));
        
        return modelAndView;
    }
    
    /**
     * On create product category
     * 
     * @param productCategory
     * @return
     */
    @RequestMapping(value = "/product-categories", method = RequestMethod.POST)
    public @ResponseBody String onCreate(ProductCategory productCategory) {
        
        Result result = new Result();
        
        try {
            
            productCategoryService.save(productCategory);
            
            result.success().message("创建产品分类成功！");
            
        } catch(Exception e) {
            
            logger.error("#onCreate() - ", e);
            
            result.fail().message("创建产品分类失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * View category edit panel
     * 
     * @param productCategoryId
     * @return
     */
    @RequestMapping(value = "/product-categories/{productCategoryId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("productCategoryId") Long productCategoryId) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/category/product-category-edit");
        
        ProductCategory productCategory = productCategoryService.load(ProductCategory.class, productCategoryId);
        
        modelAndView.addObject("productCategory", productCategory);
        
        return modelAndView;
    }

    /**
     * Do edit product category
     * 
     * @param productCategory
     * @param cover
     * @return
     */
    @RequestMapping(value = "/product-categories/{productCategoryId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("productCategoryId") ProductCategory productCategory,
                                       @RequestParam(name = "inputCover") String cover) {
        
        Result result = new Result();
        
        try {
            
            productCategoryService.update(productCategory.editCover(cover));
            
            result.success().message("修改产品分类成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改产品分类失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    /**
     * View category delete panel
     * 
     * @param productCategoryId
     * @return
     */
    @RequestMapping(value = "/product-categories/{productCategoryId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("productCategoryId") Long productCategoryId) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/product/category/product-category-delete");
        
        modelAndView.addObject("productCategoryId", productCategoryId);
        
        return modelAndView;
    }
    
    /**
     * Do delete product category
     * 
     * @param productCategoryId
     * @return
     */
    @RequestMapping(value = "/product-categories/{productCategoryId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("productCategoryId") Long productCategoryId) {
        
        Result result = new Result();
        
        try {
            
            ProductCategory productCategory = productCategoryService.load(ProductCategory.class, productCategoryId);
            
            productCategoryService.update(productCategory.disable());
            
            result.success().message("删除产品分类成功！");
            
        } catch(Exception e) {
            
            logger.error("#onDelete() - ", e);
            
            result.fail().message("删除产品分类失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    
}
